package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjFactoryEmp;
import com.zb.extend.FactoryEmpExtend;
import com.zb.mapper.ZjFactoryEmpMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjFactoryEmpService;
import com.zb.vo.FactoryEmpListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 厂商人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-27
 */
@Service
public class ZjFactoryEmpServiceImpl extends ServiceImpl<ZjFactoryEmpMapper, ZjFactoryEmp> implements ZjFactoryEmpService {
    @Resource
    private ZjFactoryEmpMapper factoryEmpMapper;

    // 保存更改品牌厂商人员
    @Override
    public void saveFactoryAdmin(Long factoryId, Long accountId) {
        QueryWrapper<ZjFactoryEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "factory_account_id")
                .eq("factory_id", factoryId).eq("is_super_account", true);
        ZjFactoryEmp factoryEmp = this.getOne(wrapper);
        // 不存在则新增
        if (null == factoryEmp) {
            factoryEmp = new ZjFactoryEmp();
            factoryEmp.setId(IdWorker.getId());
            factoryEmp.setFactoryId(factoryId);
            factoryEmp.setFactoryAccountId(accountId);
            factoryEmp.setSuperAccount(true);
            factoryEmp.setDepart(false);
            this.save(factoryEmp);
            return;
        }
        // 判断是否同一账号
        if (Objects.equals(accountId, factoryEmp.getFactoryAccountId())) {
            return;
        }
        // 查询账号是否存在
        wrapper = new QueryWrapper<>();
        wrapper.select("id", "factory_account_id")
                .eq("factory_id", factoryId).eq("factory_account_id", accountId);
        ZjFactoryEmp emp = this.getOne(wrapper);

        List<ZjFactoryEmp> updates = new ArrayList<>();
        if (null != emp) {
            factoryEmp.setSuperAccount(false);
            updates.add(factoryEmp);

            emp.setSuperAccount(true);
            updates.add(emp);
        } else {
            factoryEmp.setFactoryAccountId(accountId);
            updates.add(factoryEmp);
        }
        this.updateBatchById(updates);
    }

    // 获取商家详情之账号管理
    @Override
    public Map<String, Object> getFactoryEmpAccountList(Long factoryId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<FactoryEmpExtend> empExtends = factoryEmpMapper.getFactoryEmpAccountList(factoryId, selectVo);
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), factoryEmpMapper.getFactoryEmpAccountListCount(factoryId, selectVo),
                empExtends.stream().map(empExtend -> {
                    FactoryEmpListVo vo = new FactoryEmpListVo();
                    BeanUtil.copyProperties(empExtend, vo);
                    vo.setManager(empExtend.getSuperAccount());
                    vo.setCreateTime(empExtend.getGmtCreate());
                    vo.setUpdateTime(empExtend.getGmtUpdate());
                    return vo;
                }).toList());
    }
}
